import Vue from "vue";
import Router from "vue-router";
import store from "store";
import Home from "./views/Home.vue";
import Login from "./views/Login.vue";
import Test from "./views/test.vue";
import Layout from "@/components/Layout.vue"; //菜单
import lmanage from "@/views/system/lmanage.vue";
import subscribe from "@/views/system/subscribe.vue";
import equipment from "@/views/system/equipment.vue";
import netshop from "@/views/BusHall/netshop.vue";
import register from "@/views/BusHall/register.vue";
import MakeTable from "@/views/tableReport/MakeTable.vue";

Vue.use(Router);
const routess = [
  {
    path: "/",
    name: "home",
    component: Home,
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/test",
    name: "test",
    component: Test,
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "./views/About.vue"),
  },
  {
    path: "/Layout",
    name: "系统管理",
    component: Layout,
    icon: "systemCom",
    redirect:'/system/lmanage',
    children: [
      {
        path: "/system/lmanage",
        component: lmanage,
        icon: "deptCom",
        name: "陵园管理",
      },
      {
        path: "/system/subscribe",
        component: subscribe,
        icon: "deptCom",
        name: "预约活动管理",
      },
      {
        path: "/system/equipment",
        component: equipment,
        icon: "deptCom",
        name: "设备管理",
      },
    ],
  },
  {
    path: "/BusHall",
    name: "业务大厅",
    component: Layout,
    icon: "roomCom",
    redirect:'/BusHall/netshop',
    children: [
      {
        path: "/BusHall/netshop",
        component: netshop,
        icon: "deptCom",
        name: "网上祭祀",
      },
      {
        path: "/BusHall/register",
        component: register,
        icon: "deptCom",
        name: "入园登记",
      },
    ],
  },
  {
    path: "/tableReport",
    name: "报表统计",
    component: Layout,
    icon: "roomCom",
    redirect:'/tableReport/MakeTable',
    children: [
      {
        path: "/tableReport/MakeTable",
        component: MakeTable,
        icon: "deptCom",
        name: "预约报表",
      },
    ],
  },
];
const router = new Router({
  mode: "hash",
  base: process.env.BASE_URL,
  routes: routess,
});

export default router;

router.beforeEach((to, from, next) => {
  let hasMenus = false;
  // const token = store.state.login.userInfo ? store.state.login.userInfo.token : null

  const token = store.state.login.userInfo
    ? store.state.login.userInfo.token
    : null;
  const nextRouter = ["/login", "/improt"];
  if (to.path === "/login") {
    next();
  } else {
    if (token) {
      next();
    } else {
      next("/login");
    }
  }
});
